/**
 * [697] 数组的度
 *
 * @format
 * @lc app=leetcode.cn id=697 lang=javascript
 */

// @lc code=start
/**
 * @param {number[]} nums
 * @return {number}
 */
var findShortestSubArray = function (nums) {
    const dist = {}
    let [degMax, lenMin] = [1, nums.length]
    nums.forEach((e, i) => {
        dist[e]
            ? (dist[e].count++, (dist[e].right = i))
            : (dist[e] = { count: 1, left: i, right: i })
        dist[e].count > degMax ? (degMax = dist[e].count) : null
    })
    for (let num in dist) {
        if (dist[num].count == degMax) {
            const len = dist[num].right - dist[num].left + 1
            len < lenMin ? (lenMin = len) : null
        }
    }
    return lenMin
}
// debugger
// console.log(findShortestSubArray([1, 2, 2, 3, 1]))
// @lc code=end
